package henu.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import henu.bean.Lesson;

import henu.dao.ILessonDao;
/**
 * @author 纪柳任
 * 2016年9月19日下午10:11:21
 */
public class LessonDaoImpl implements ILessonDao{


	protected QueryRunner queryRunner = new QueryRunner();
	private int rows;

	public LessonDaoImpl(QueryRunner queryRunner) {
		super();
		this.queryRunner = queryRunner;
	}

	@Override
	public Lesson get(String fClassNumber) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_lesson where fClassNumber=?";
		Object param[]={fClassNumber};
		//lessons是返回的对象
		Lesson lessons= null;
		try {
			lessons= queryRunner.query(sql, param,new BeanHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
	}

	@Override
	public int add(Lesson lesson) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_lesson(fClassName,fClassNumber,fTheoryTime,fTestTime,fStartWeek,fEndWeek,fSchoolYear,fSchoolTerm,fRemark,fTeacher) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] param ={lesson.getfClassName(),lesson.getfClassNumber(),lesson.getfTheoryTime(),lesson.getfTestTime(),lesson.getfStartWeek(),
				lesson.getfEndWeek(),lesson.getfSchoolYear(),lesson.getfSchoolTerm(),lesson.getfRemark(),lesson.getfTeacher()};
		
		try {
			rows=queryRunner.update(sql, param);
       // System.out.println(rows+"行已经发生变化");
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return rows;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		String sql="delete from tb_lesson where fClassNumber=?";
		Object[] param={id};
		try {
			rows=queryRunner.update(sql,param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	@Override
	public int update(Lesson lesson) {
		// TODO Auto-generated method stub
		String sql="update tb_lesson set fTestTime=?,fStartWeek=?,fEndWeek=?,fSchoolYear=?,fSchoolTerm=?,fRemark=? where fTheoryTime=?";
		Object param[]={lesson.getfTestTime(),lesson.getfStartWeek(),lesson.getfEndWeek(),lesson.getfSchoolYear(),lesson.getfSchoolTerm(),lesson.getfRemark(),lesson.getfTheoryTime()};
		try {
			rows=queryRunner.update(sql,param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;

	}

	@Override
	public List<Lesson> getAll() {
		// TODO Auto-generated method stub
		String sql = "select * from tb_lesson";
		List<Lesson> lessons= null;		
		try {
			lessons = queryRunner.query(sql, new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
	}
	
	@Override
	public List<Lesson> getAll(int pageSize, int pageNow) {
		String sql = "select * from tb_lesson limit "+(pageNow*pageSize-pageSize)+","+pageSize;
		List<Lesson> lessons= null;		
		try {
			lessons = queryRunner.query(sql, new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
	}
	
	@Override
	public List<Lesson> getById(String id) {
		String sql = "select * from tb_lesson where fClassNumber=?";
		Object param[] = {id};
		List<Lesson> lessons = null;
		try {
			lessons = queryRunner.query(sql, param,new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
		
	}

	@Override
	public List<Lesson> getByName(String name) {
		String sql = "select * from tb_lesson where fClassName=?";
		Object param[] = {name};
		List<Lesson> lessons = null;
		try {
			lessons = queryRunner.query(sql, param,new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
	}

	@Override
	public List<Lesson> getByYear(String year) {
		String sql = "select * from tb_lesson where fSchoolYear=?";
		Object param[] = {year};
		List<Lesson> lessons = null;
		try {
			lessons = queryRunner.query(sql, param,new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons;
	}

	@Override
	public int getCount() {
		String sql = "select * from tb_lesson";
		List<Lesson> lessons= null;		
		try {
			lessons = queryRunner.query(sql, new BeanListHandler<Lesson>(Lesson.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lessons.size();
	}

	

}
